using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using FinlogiK.FxCop.Statistics;
using FinlogiK.FxCop.Statistics.Windows.Data;
using FinlogiK.FxCop.Statistics.Windows.Data.ProjectDataSetTableAdapters;


namespace FinlogiK.FxCop.Statistics.Windows
{
	internal partial class StatisticsViewerForm : Form
	{
		#region Constructors
		
		internal StatisticsViewerForm() : this(null, null)
		{
		}

		internal StatisticsViewerForm(int? projectId, DateTime? analysisTimestamp)
		{
			this.InitializeComponent();

			this.totalSummaryGrid.AutoGenerateColumns = false;
			this.totalDetailsGrid.AutoGenerateColumns = false;
			this.todoSummaryGrid.AutoGenerateColumns = false;
			this.todoDetailsGrid.AutoGenerateColumns = false;

			new ProjectsTableAdapter().Fill(this.projectDataSet.Projects, null);
			new AnalysisRunsTableAdapter().Fill(this.projectDataSet.AnalysisRuns);

			this.BindCriteriaDropDowns(projectId, analysisTimestamp);
		}

		#endregion

		#region Methods

		private void BindCriteriaDropDowns(int? projectId, DateTime? analysisTimestamp)
		{
			this.projectIdDropDown.DisplayMember = "ProjectName";
			this.projectIdDropDown.ValueMember = "ProjectId";

			this.analysisTimestampDropDown.DisplayMember = "AnalysisTimestamp";
			this.analysisTimestampDropDown.ValueMember = "AnalysisTimestamp";
			
			this.projectIdDropDown.DataSource = this.projectDataSet.Projects;

			if (projectId != null)
			{
				this.projectIdDropDown.SelectedValue = projectId.Value;

				if (analysisTimestamp != null)
				{
					this.analysisTimestampDropDown.SelectedValue = analysisTimestamp.Value;
					this.DisplayStatistics(projectId, analysisTimestamp);
				}
			}
		}

		private void projectIdDropDown_SelectedValueChanged(object sender, EventArgs e)
		{
			this.DisplayStatistics(null, null);

			int? projectId = (int?)this.projectIdDropDown.SelectedValue;
			if (projectId == null)
			{
				this.analysisTimestampDropDown.DataSource = null;
			}
			else
			{
				List<ProjectDataSet.AnalysisRunsRow> analysisRuns = new List<ProjectDataSet.AnalysisRunsRow>(this.projectDataSet.Projects.FindByProjectId(projectId.Value).GetAnalysisRunsRows());
				analysisRuns.Sort(new Comparison<ProjectDataSet.AnalysisRunsRow>(StatisticsViewerForm.CompareAnalysisRunsByTimestampDescending));

				analysisTimestampDropDown.DataSource = analysisRuns;
			}
		}

		private void analysisTimestampDropDown_SelectedValueChanged(object sender, EventArgs e)
		{
			int? projectId = (int?)this.projectIdDropDown.SelectedValue;
			DateTime? analysisTimestamp = (DateTime?)this.analysisTimestampDropDown.SelectedValue;

			this.DisplayStatistics(projectId, analysisTimestamp);
		}

		private void DisplayStatistics(int? projectId, DateTime? analysisTimestamp)
		{
			this.totalSummaryGrid.DataSource = null;
			this.totalDetailsGrid.DataSource = null;
			this.todoSummaryGrid.DataSource = null;
			this.todoDetailsGrid.DataSource = null;
			
			if ((projectId != null) && (analysisTimestamp != null))
			{
				StatisticsDataSet totalDataSet = new StatisticsDataSet();
				totalDataSet.Fill(projectId.Value, analysisTimestamp.Value, ViolationStatisticType.Total);

				this.totalSummaryGrid.DataSource = totalDataSet.ViolationSummary;
				this.totalDetailsGrid.DataSource = totalDataSet.ViolationDetails;

				StatisticsDataSet todoDataSet = new StatisticsDataSet();
				todoDataSet.Fill(projectId.Value, analysisTimestamp.Value, ViolationStatisticType.Todo);

				this.todoSummaryGrid.DataSource = todoDataSet.ViolationSummary;
				this.todoDetailsGrid.DataSource = todoDataSet.ViolationDetails;
			}
		}

		private static int CompareAnalysisRunsByTimestampDescending(ProjectDataSet.AnalysisRunsRow x, ProjectDataSet.AnalysisRunsRow y)
		{
			int result;

			if (x == null)
			{
				if (y == null)
				{
					result = 0;
				}
				else
				{
					result = -1;
				}
			}
			else if (y == null)
			{
				result = 1;
			}
			else
			{
				if (x.AnalysisTimestamp == y.AnalysisTimestamp)
				{
					result = 0;
				}
				else if (x.AnalysisTimestamp > y.AnalysisTimestamp)
				{
					result = -1;
				}
				else
				{
					result = 1;
				}
			}

			return result;
		}

		#endregion
	}
}